package com.views;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

public class CustomProgressBar extends View {
	private static final int THICK_NESS_DP = 6;
	private int startColor = Color.parseColor("#11000000");
	private int progressColor = Color.WHITE;
	private float progress = 0;
	private Paint paint;
	private RectF rectF;
	private float startConer;
	private Path path;

	private int thickness;

	public void setThickness(int pixel) {
		thickness = pixel;
		invalidate();
	}

	public void setProgressColor(int color) {
		progressColor = color;
		invalidate();
	}

	public void setRingColor(int color) {
		startColor = color;
		invalidate();
	}

	public void setProgress(float progress) {
		this.progress = progress;
		invalidate();
	}

	public CustomProgressBar(Context context) {
		super(context);
		init();
	}

	public CustomProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public CustomProgressBar(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	private void init() {
		thickness = (int) convertDpToPixel(THICK_NESS_DP, getContext());
		paint = new Paint();

		rectF = new RectF();

		path = new Path();

		startConer = -90;
	}

	public static float convertDpToPixel(float dp, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return px;
	}

	public static float convertPixelsToDp(float px, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float dp = px / (metrics.densityDpi / 160f);
		return dp;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int width = getWidth();

		float radius = (width - thickness) / 2;
		float center_x, center_y;
		center_x = width / 2;
		center_y = width / 2;
		float endConer = 360.0f * progress / 100.0f;

		path.addCircle(width / 2, width / 2, (width - thickness) / 2,
				Path.Direction.CCW);

		paint.setColor(startColor);
		paint.setStrokeWidth(thickness);
		paint.setStyle(Paint.Style.STROKE);
		canvas.drawCircle(center_x, center_y, radius, paint);

		paint.setColor(progressColor);
		paint.setStrokeWidth(thickness);
		paint.setStyle(Paint.Style.STROKE);
		rectF.set(center_x - radius, center_y - radius, center_x + radius,
				center_y + radius);
		canvas.drawArc(rectF, startConer, endConer, false, paint);

		super.onDraw(canvas);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		int width = getMeasuredWidth();
		setMeasuredDimension(width, width);
	}

}
